package com.xbongbong.paas.script.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.script.pojo.IPojo;
import com.xbongbong.paas.script.pojo.ListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.constant.ElasticConstant;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/9/12 14:50
 * @since v1.0
 */
@Component
public class ScriptEsHelper {
    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;

    @SuppressWarnings("all")
    public <T>ListVO<T> list(BoolQueryBuilder boolQueryBuilder, Integer pageSize, Class<T> clazz, IndexTypeEnum indexTypeEnum, Long dataIdMark) throws XbbException {
        return list(boolQueryBuilder,pageSize,clazz,indexTypeEnum,dataIdMark,null);
    }

    @SuppressWarnings("all")
    public <T>ListVO<T> list(BoolQueryBuilder boolQueryBuilder, Integer pageSize, Class<T> clazz, IndexTypeEnum indexTypeEnum, Long dataIdMark,List<String> fieldList) throws XbbException {
        //id标记
        boolQueryBuilder.filter(rangeQuery("id").gt(dataIdMark));
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD,indexTypeEnum.getType()));
        //索引
        SearchRequest searchRequest = new SearchRequest(indexTypeEnum.getIndex());
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(boolQueryBuilder);
        //限定返回值
        if (Objects.nonNull(fieldList)){
            //下面取idMark要用
            List<String> newFieldList = new ArrayList<>(fieldList);
            newFieldList.add("id");
            //有问题找海滨
//            EsUtil.setFieldList(sourceBuilder, newFieldList);
            //处理显示字段
            EsUtil.setFieldListNoMerge(sourceBuilder, newFieldList);
        }
        searchRequest.source(sourceBuilder);
        //id排序
        sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC));
        //分页
        PageRequest pageRequest = PageRequest.of(1, pageSize);
        sourceBuilder.from(0);
        sourceBuilder.size(pageSize);
        ListVO listVO = new ListVO();
        List<T> list = xbbElasticsearchRestTemplate.queryForPages(pageRequest, searchRequest, clazz).getContent();
        Long nextDataIdMark = -1L;
        if (!list.isEmpty()){
            int size = list.size();
            IPojo iPojo = JSONObject.parseObject(JSONObject.toJSONString(list.get(size-1)),IPojo.class);
            nextDataIdMark = iPojo.getId();
        }
        listVO.setList(list);
        listVO.setDataIdMark(nextDataIdMark);
        return listVO;
    }
}
